import 'package:flutter/material.dart';
import 'package:flutter_mwm/util/util.dart';

///使用dialog或者progressDialog的时候⚠注意全局context和当前控件的context
///提示语
class PromptDialog {
  static void show(BuildContext buildContext, String message,
      {String btnText,
      VoidCallback callback,
      bool outsideDismiss = true,
      bool exit = false}) {
    if (null == buildContext) return;
    showDialog<bool>(
      context: buildContext,
      barrierDismissible: exit ? false : outsideDismiss,
      builder: (BuildContext context) {
        return AlertDialog(
          shape: RoundedRectangleBorder(
            borderRadius: BorderRadius.all(
              Radius.circular(8.0),
            ),
          ),
          title: Text('温馨提示'),
          contentPadding: EdgeInsets.only(left: 24.0, top: 10.0, right: 24.0),
          content: Text(TextUtils.replace(message, ''),
              maxLines: 5, style: TextStyle(color: Colors.red, fontSize: 18.0)),
          actions: <Widget>[
            FlatButton(
              textColor: Colors.black,
              child: Text(
                TextUtils.isEmpty(btnText) ? "确 定" : btnText,
                style: TextStyle(fontSize: 18.0),
              ),
              onPressed: () {
                //1
                /*if (null != callback) {
                  _dismiss().then((_) {
                    callback();
                  });
                } else {
                  _dismissDialog(context);
                }*/

                //2
                /*exit
                    ? _dismiss().then((_) {
                        Navigator.pop(context);
                      })
                    : _dismissDialog(context);*/
                if (null != callback) {
                  _dismiss(buildContext).then((_) {
                    callback();
                  });
                } else {
                  exit
                      ? _dismiss(buildContext).then((_) {
                          Navigator.pop(context);
                        })
                      : _dismissDialog(context);
                }
              },
            ),
          ],
        );
      },
    );
  }

  static Future<bool> _dismiss(BuildContext context) async {
    if (null != context) Navigator.pop(context);
    return true;
  }

  static Future<bool> _dismissDialog(BuildContext context) async {
    if (null != context) Navigator.pop(context);
    return true;
  }
}

enum Operate {
  cancel,
  next,
  all,
}

///使用dialog或者progressDialog的时候⚠注意全局context和当前控件的context
///操作Dialog
class OperateDialog {
  final BuildContext buildContext;
  final String message;
  final String cancelText;
  final String nextText;
  final bool outsideDismiss;
  final bool exit;
  final Operate operate;
  final VoidCallback cancelCall;
  final VoidCallback nextCall;

  const OperateDialog(this.buildContext, this.message,
      {this.cancelText,
      this.nextText,
      this.outsideDismiss = true,
      this.exit = false,
      this.operate,
      this.cancelCall,
      this.nextCall})
      : assert(null != buildContext);

  void show() {
    if (null == buildContext) return;
    bool _exit = null == exit ? false : exit;
    bool _outsideDismiss =
        _exit ? false : (null == outsideDismiss ? true : false);

    showDialog<bool>(
      context: buildContext,
      barrierDismissible: _outsideDismiss,
      builder: (BuildContext context) {
        return AlertDialog(
          shape: RoundedRectangleBorder(
            borderRadius: BorderRadius.all(
              Radius.circular(8.0),
            ),
          ),
          title: Text('温馨提示'),
          contentPadding: EdgeInsets.only(left: 24.0, top: 10.0, right: 24.0),
          content: Text(TextUtils.replace(message, ''),
              maxLines: 5, style: TextStyle(color: Colors.red, fontSize: 18.0)),
          actions: _child(operate, context),
        );
      },
    );
  }

  _child(Operate operate, BuildContext context) {
    if (null == operate) operate = Operate.all;
    List<Widget> children = List();

    Widget cancelWidget = _cancelWidget(context);
    Widget nextWidget = _nextWidget(context);

    if (Operate.cancel == operate) {
      children.clear();
      children.add(cancelWidget);
    } else if (Operate.next == operate) {
      children.clear();
      children.add(nextWidget);
    } else {
      children.add(cancelWidget);
      children.add(nextWidget);
    }

    return children;
  }

  Widget _cancelWidget(BuildContext context) {
    String _cancelText = TextUtils.replace(cancelText, '取 消');
    return FlatButton(
      textColor: Colors.black,
      child: Text(
        _cancelText,
        style: TextStyle(fontSize: 18.0),
      ),
      onPressed: () {
        if (null != cancelCall) {
          _dismiss(buildContext).then((_) {
            cancelCall();
          });
        } else {
          _dismiss(context);
        }
      },
    );
  }

  Widget _nextWidget(BuildContext context) {
    String _nextText = TextUtils.replace(nextText, '确 定');

    return FlatButton(
      textColor: Colors.black,
      child: Text(
        _nextText,
        style: TextStyle(fontSize: 18.0),
      ),
      onPressed: () {
        if (null != nextCall) {
          _dismiss(buildContext).then((_) {
            nextCall();
          });
        } else {
          exit
              ? _dismiss(buildContext).then((_) {
                  Navigator.pop(context);
                })
              : _dismiss(context);
        }
      },
    );
  }

  Future<bool> _dismiss(BuildContext context) async {
    if (null != context) Navigator.pop(context);
    return true;
  }
}
